using System;
using System.Collections.Generic;
using System.Linq;

namespace Baula.TextGen.ActiveTemplating
{
    public interface ITableInfo
    {
        string Name { get; }
        string FieldNames { get; set; }
        IList<IFieldInfo> Fields { get; }
    }

    public class TableInfo : ITableInfo
    {
        private IList<IFieldInfo> _fields = new List<IFieldInfo>();

// ReSharper disable UnusedMember.Global
        public TableInfo()
        {
        }
// ReSharper restore UnusedMember.Global

        public TableInfo(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
        private string _fieldNames;
        public string FieldNames
        {
            get { return _fieldNames; }
            set
            {
                if (_fields.Count > 0)
                    throw new Exception("FieldNames is readonly if there are already any fields assigned to the table.");
                var fields = value.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => new FieldInfo(s.Trim()));
                _fields = new List<IFieldInfo>(fields);
            }
        }

        public IList<IFieldInfo> Fields
        {
            get { return _fields; }
        }
    }
}